package com.gzw.dms.adapter;

import android.app.AlertDialog;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.gzw.dms.R;
import com.gzw.dms.bean.MapInfo;
import com.gzw.dms.utils.DateUtil;
import com.gzw.dms.utils.L;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by GaoZhiWei on 2016/2/23.
 */
public class PersonAdapter extends RecyclerView.Adapter {
    private  String    now;
    public static interface OnRecyclerViewListener {
        void onItemClick(int position);

        boolean onItemLongClick(int position);
    }

    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    private static final String TAG = PersonAdapter.class.getSimpleName();
    private List<MapInfo> list;

    public PersonAdapter(List<MapInfo> list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        L.d(TAG, "onCreateViewHolder, i: " + i);

        SimpleDateFormat    formatter    =   new SimpleDateFormat("yyyy-MM-dd    HH:mm:ss ");
        Date    curDate    =   new Date(System.currentTimeMillis());//获取当前时间
        now    =    formatter.format(curDate);
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_vehicle, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);
        return new PersonViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        L.d(TAG, "onBindViewHolder, i: " + i + ", viewHolder: " + viewHolder);
        PersonViewHolder holder = (PersonViewHolder) viewHolder;
        holder.position = i;
        MapInfo mapInfo = list.get(i);
        holder.tv_carno.setText("用户名:"+"  "+mapInfo.getCarno());
        holder.tv_drivename.setText("司机姓名:"+"  "+mapInfo.getDrivename());
        holder.tv_phone.setText("司机电话:"+"  "+mapInfo.getDrivenumber());


        String old = mapInfo.getNowdate();


        if (mapInfo.getGpsstate().equals("APP")){
            try {
                String result = DateUtil.minuteCompare(old, now);

                if (result == "Wone") {
                    holder.tv_state.setText("状态: App在线");

                } else if (result == "Wtwo") {
                    holder.tv_state.setText("状态: App离线");

                } else {
                    holder.tv_state.setText("状态: App离线");

                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }else {
            if (mapInfo.getState().equals("3") || mapInfo.getState().length() == 0) {
                holder.tv_state.setText("状态: GPS离线");
            } else if(mapInfo.getState().equals("2")){
                holder.tv_state.setText("状态: GPS离线");
            }else{
                holder.tv_state.setText("状态: GPS在线");
            }
        }

        holder.tv_dress.setText("司机地址:"+"  "+mapInfo.getAddress());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class PersonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        public View rootView;
        public TextView tv_carno;
        public TextView tv_drivename;
        public TextView tv_phone;
        public TextView tv_state;
        public TextView tv_dress;
        public int position;

        public PersonViewHolder(View itemView) {
            super(itemView);
            tv_carno = (TextView) itemView.findViewById(R.id.tv_carno);
            tv_drivename = (TextView) itemView.findViewById(R.id.tv_drivename);
            tv_phone = (TextView) itemView.findViewById(R.id.tv_phone);
            tv_state = (TextView) itemView.findViewById(R.id.tv_state);
            tv_dress = (TextView) itemView.findViewById(R.id.tv_dress);
            rootView = itemView.findViewById(R.id.ll);
            rootView.setOnClickListener(this);
            rootView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (null != onRecyclerViewListener) {
                onRecyclerViewListener.onItemClick(position);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != onRecyclerViewListener) {
                return onRecyclerViewListener.onItemLongClick(position);
            }
            return false;
        }
    }
}